package com.qf.smsplatform.webmaster.controller.advice;

import com.qf.smsplatform.webmaster.constans.ResultCode;
import com.qf.smsplatform.webmaster.dto.R;
import com.qf.smsplatform.webmaster.exceptions.MyBaseException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice// 当前类是对controller进行aop增强的通知类
@ResponseBody//当前类中所有的方法返回值都是R,都会通过响应正文返回
public class MyExceptionAdvice {


    @ExceptionHandler(MyBaseException.class)//当前注解的作用是说明当前方法用于处理MyBaseException类型的异常
    public R processMyBaseException(MyBaseException exception) {
        exception.printStackTrace();
        int code = exception.getCode();
        String msg = exception.getMessage();
        return R.setError(code, msg, null);//返回R,这样调用者就不会收到异常了
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R processHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
        return R.setError(ResultCode.HTTP_METHOD_NOT_ALLOW, "请求方式不允许", null);//返回R,这样调用者就不会收到异常了
    }

    @ExceptionHandler(Exception.class)
    public R processException(Exception exception) {
        exception.printStackTrace();
        return R.setError(null);
    }
}
